package zjhr.com.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import zjhr.com.pojo.TUser;
import zjhr.com.service.LoginService;
import zjhr.com.utils.TaotaoResult;


@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	//登录功能
	@RequestMapping("/login.action")
	public String Login(String username,String password,String checkcode,HttpServletRequest request,Model model){
		 String key = (String) request.getSession().getAttribute("key");
		 if(StringUtils.isNotBlank(checkcode) && checkcode.equals(key)){
			 TUser user = loginService.findUserByUserNamePassword(username, password);
			 if(user !=null){
				 request.getSession().setAttribute("user", user);
				 return "/common/index.jsp";
			 }else{
				 request.getSession().setAttribute("error", "账号或者密码输入错误");
				 return "forward:login.jsp"; 
			 }
		 }else{
			 request.getSession().setAttribute("error", "验证码输入错误");
			 return "forward:login.jsp"; 
		 }
		// return "redirect:/index.action";
	}
	
	//退出功能
	@RequestMapping("/layout.action")
	public String layout(HttpServletRequest request){
		request.getSession().invalidate();
		return "redirect:login.jsp";
	}
	
	//修改密码功能
	@RequestMapping("/editPassword.action")
	@ResponseBody
	public TaotaoResult editPassword(String id,String password){
		TaotaoResult result = loginService.updatePasswordById(id, password);
		return result;
	}

}
